﻿using System;
using System.ComponentModel;
using MECPSDB.EntityClasses;
using MECPSDB.FactoryClasses;
using MECPSDB.HelperClasses;
using MECUITMPLTLIB;
using MECPSDB.DatabaseSpecific;
using SD.LLBLGen.Pro.ORMSupportClasses;

namespace MEC.WMS.FileMaintenance.MatBrand {
    
    public partial class AeFrm:AeFrmUiTemplate{
        
        private const string Caption = "Brand Maintenance";
        
        public AeFrm(string cs, string branchcode, string usercode){
            if (cs == null){
                throw new ArgumentNullException("cs");
            }
            Cs= cs;
            Branchcode = branchcode;
            Usercode = usercode;
            InitializeComponent();
            this.Text = Caption;
        }

        public override sealed string Text{
            get{
                return base.Text;
            }
            set{
                base.Text = value;
            }
        }

        public override void MethodNewEntityProcedure(){
           base.MethodNewEntityProcedure();
           //todo set default for the newly added entity by using another object
           MatbrandEntity mainEntity = new MatbrandEntity(); //-implementation
           //todo Add extra code here
           //... 
           //todo assign the new entity to the entity object bounded to the form 
           MainEntity = mainEntity; //-implementation
           //todo must put at the end when there is no error - allow edit to true
           MethodAllowEdit(true); ////-implementation
       }

        protected override void AeFrmUiTemplateLoad(object sender, EventArgs e){
        //implements generic setting of Dexex controls behavior
            base.AeFrmUiTemplateLoad(sender,e); 
        //todo
        }

        protected override void MainEntityBackgroundSaveWorkCompleted(object sender, RunWorkerCompletedEventArgs e){
            base.MainEntityBackgroundSaveWorkCompleted(sender,e);
        //todo
        }

        protected override void MainEntityBackgroundSaveDoWork(object sender, DoWorkEventArgs e){
            //todo declare with correct entity type
            MatbrandEntity entity = (MatbrandEntity) e.Argument;
            using (DataAccessAdapter dataAccessAdapter = new DataAccessAdapter(Cs)){

                if (entity.IsNew){} else{}

                dataAccessAdapter.SaveEntity(entity, true);
                e.Result = entity;
            }

        }

        protected override void MethodLoadControlValuesToObjectsForSaving(){
            MatbrandEntity entity = (MatbrandEntity) MainEntity;
            entity.BrandCode = (string) textEditBrandcode_ae_pk_.EditValue;
            entity.BrandDescription = (string) textEditDescription_ae_.EditValue;
            entity.Rem = (string) memoEditRemarks_ae_.EditValue;
       }

        protected override bool MethodValidate(){
            dxErrorProvider1.ClearErrors();
            bool focused = false;
            //tododone
            if (string.IsNullOrWhiteSpace(textEditBrandcode_ae_pk_.Text)){
                dxErrorProvider1.SetError(textEditBrandcode_ae_pk_, "Required");
                textEditBrandcode_ae_pk_.Focus();
                focused = true;
            }
            if (string.IsNullOrWhiteSpace(textEditDescription_ae_.Text)){
                dxErrorProvider1.SetError(textEditDescription_ae_, "Required");
                if (!focused){
                    textEditDescription_ae_.Focus();
                    focused = true;
                }
            }

            return !dxErrorProvider1.HasErrors;
        }

        protected override void MethodClearForm(){
        //tododone
            base.MethodClearForm();
            //todone extra handling
            
        }
        protected override void MethodDispayMainEntityToControls(){
        //tododone
            base.MethodDispayMainEntityToControls();
            MatbrandEntity material = (MatbrandEntity) MainEntity;
            textEditBrandcode_ae_pk_.EditValue = material.BrandCode;
            textEditDescription_ae_.EditValue = material.BrandDescription;
            memoEditRemarks_ae_.EditValue = material.Rem;
        }

        protected override void MainEntityFetchProgressChanged(object sender, ProgressChangedEventArgs e){
            base.MainEntityFetchProgressChanged(sender, e);
            //no lookup
            //EntityCollection ec = (EntityCollection) e.UserState;
            //if (ecbrand.EntityFactoryToUse.GetType() == ec.EntityFactoryToUse.GetType()){
            //    ecbrand = ec;
            //    lookUpEditBrandCode_ae_.Properties.DataSource = ec;
            //}else
            //if (ecgroup.EntityFactoryToUse.GetType() == ec.EntityFactoryToUse.GetType()){
            //    ecgroup = ec;
            //    lookUpEditGroupCode_ae_.Properties.DataSource = ec;
            //}else
            //if (ecuom.EntityFactoryToUse.GetType() == ec.EntityFactoryToUse.GetType()){
            //    ecuom = ec;
            //    lookUpEditUomCode_ae_.Properties.DataSource = ec;
            //}           

        }
        protected override void MainEntityBackgroundFetchDoWork(object sender, DoWorkEventArgs e){
            FetchParam fetchParam = (FetchParam) e.Argument;
            BackgroundWorker bw = (BackgroundWorker) sender;
            //TODOdone - No LookupFetches
            //TODO MainEntityFetch
            if (fetchParam.FetchMainEntity){
                MatbrandEntity entity = new MatbrandEntity(fetchParam.PkCode);
                IPrefetchPath2 prefetchPath = null;
                ExcludeIncludeFieldsList excludedfields = new ExcludeIncludeFieldsList();
                using (DataAccessAdapter dataAccessAdapter = new DataAccessAdapter(fetchParam.Cs)){
                    dataAccessAdapter.FetchEntity(entity, prefetchPath, null, excludedfields);
                    fetchParam.MainEntityFetched = entity;
                    e.Result = fetchParam;
                    return;
                }
            }
        }


        
    }//End class
} // End namespace